package cn.stronglink.asset.module.assetmanage.borrowreturn.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.AssetRecord;
import cn.stronglink.asset.model.BorrowReturn;
import cn.stronglink.asset.model.BorrowReturnDetail;
import cn.stronglink.asset.module.assetRecord.mapper.AssetRecordMapper;
import cn.stronglink.asset.module.assetmanage.borrowreturn.mapper.BorrowReturnMapper;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.assetmanage.transfer.vo.OriginalAssetInfo;

import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor = Exception.class)
public class BorrowReturnService {

	@Autowired
	private BorrowReturnMapper borrowReturnMapper;
	@Autowired
	private AssetRecordMapper assetRecordMapper;
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Page<BorrowReturn> queryBorrowReturnList(Page<BorrowReturn> page, Map<String, Object> params) {
		List<BorrowReturn> borrowReturnList = borrowReturnMapper.queryBorrowReturnList(page, params);
		page.setRecords(borrowReturnList);
		return page;
	}

	public void remove(Long id) {
		borrowReturnMapper.delBorrowReturnDetailByBorrowReturnId(id);
		borrowReturnMapper.remove(id);
	}

	public void create(BorrowReturn borrowReturn) {
		// 迁移单
		if (borrowReturn.getId() == null) {
			borrowReturn.setId(IdWorker.getId());
		}
		borrowReturn.setStatus(1);
		borrowReturn.setCreateBy(WebUtil.getCurrentUser());
		borrowReturn.setCreateTime(new Date());
		borrowReturnMapper.insertBorrowReturn(borrowReturn);
		if (borrowReturn.getBorrowReturnDetail() != null && borrowReturn.getBorrowReturnDetail().size() > 0) {
			for (BorrowReturnDetail detail : borrowReturn.getBorrowReturnDetail()) {
				OriginalAssetInfo assetInfo = borrowReturnMapper.getAsset(Long.valueOf(detail.getAssetId_()));
				detail.setBorrowReturnId(borrowReturn.getId());
				if (assetInfo != null) {
					detail.setOriginalManageBranchId(assetInfo.getOriginalManageBranchId());
					detail.setOriginalUseBranchId(assetInfo.getOriginalUseBranchId());
					detail.setOriginalEndUser(assetInfo.getOriginalEndUser());
				}
				detail.setStatus(1);
				detail.setId(IdWorker.getId());
				detail.setCreateBy(WebUtil.getCurrentUser());
				detail.setCreateTime(new Date());
				borrowReturnMapper.insertBorrowReturnDetail(detail);
			}
		}
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public BorrowReturn qryById(Long id) {
		BorrowReturn borrowReturn = borrowReturnMapper.qryById(id);
		List<BorrowReturnDetail> detailList = borrowReturnMapper.qryBorrowReturnDetailByBorrowReturnId(id);
		borrowReturn.setBorrowReturnDetail(detailList);
		return borrowReturn;
	}

	public void edit(BorrowReturn borrowReturn) {
		borrowReturn.setUpdateBy(WebUtil.getCurrentUser());
		borrowReturn.setUpdateTime(new Date());
		borrowReturnMapper.update(borrowReturn);
		borrowReturnMapper.delBorrowReturnDetailByBorrowReturnId(borrowReturn.getId());
		if (borrowReturn.getBorrowReturnDetail() != null && borrowReturn.getBorrowReturnDetail().size() > 0) {
			for (BorrowReturnDetail detail : borrowReturn.getBorrowReturnDetail()) {
				OriginalAssetInfo assetInfo = borrowReturnMapper.getAsset(Long.valueOf(detail.getAssetId_()));
				detail.setBorrowReturnId(borrowReturn.getId());
				detail.setOriginalManageBranchId(assetInfo.getOriginalManageBranchId());
				detail.setOriginalUseBranchId(assetInfo.getOriginalUseBranchId());
				detail.setOriginalEndUser(assetInfo.getOriginalEndUser());
				detail.setStatus(1);
				detail.setId(IdWorker.getId());
				detail.setCreateBy(WebUtil.getCurrentUser());
				detail.setCreateTime(new Date());
				borrowReturnMapper.insertBorrowReturnDetail(detail);
			}
		}
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Page<BorrowReturnDetail> queryBorrowReturnDetailList(Page<BorrowReturnDetail> page,
			Map<String, Object> params) {
		List<BorrowReturnDetail> borrowReturnDetailList = borrowReturnMapper.queryBorrowReturnDetailList(page, params);
		page.setRecords(borrowReturnDetailList);
		return page;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public BorrowReturnDetail queryDetailById(Long id) {
		BorrowReturnDetail detail = borrowReturnMapper.queryDetailById(id);
		return detail;
	}

	public void updateToBorrow(BorrowReturn borrowReturn) {
		borrowReturnMapper.updateBorrowReturnToBorrow(borrowReturn.getId());
		if (borrowReturn.getBorrowReturnDetail() != null && borrowReturn.getBorrowReturnDetail().size() > 0) {
			for (BorrowReturnDetail detail : borrowReturn.getBorrowReturnDetail()) {
				borrowReturnMapper.updateBorrowReturnDetailToBorrowByBorrowReturnId(detail);
				
				//插入资产生命周期表记录
				 AssetRecord re = new AssetRecord();
				 re.setId(IdWorker.getId());
				 re.setAssetId(Long.parseLong(detail.getAssetId_()));
				 re.setType(5);
				 re.setCreateBy(WebUtil.getCurrentUser());
				 re.setCreateTime(new Date());
				 assetRecordMapper.create(re);

			}
		}
		List<BorrowReturnDetail> detailList = borrowReturnMapper
				.qryBorrowReturnDetailByBorrowReturnId(borrowReturn.getId());
		borrowReturnMapper.updateAssetIsBorrow(detailList);// 修改设备为借用设备
		
		for (BorrowReturnDetail borrowReturnDetail : detailList) {
			// 若为内部借用，在确认借用时，将设备归属部门修改
			if (borrowReturnDetail.getBorrowType().equals(1)) {
				borrowReturnMapper.updateAssetExtUsingTarget(borrowReturnDetail);
			}
		}
		// 外部借用时，需要再去通过上下架单去修改，这里不做改变
	}

	public void updateToReturn(BorrowReturn borrowReturn) {
		if (borrowReturn.getBorrowReturnDetail() != null && borrowReturn.getBorrowReturnDetail().size() > 0) {
			for (BorrowReturnDetail detail : borrowReturn.getBorrowReturnDetail()) {
				detail.setStatus(3);
				detail.setUpdateBy(WebUtil.getCurrentUser());
				detail.setUpdateTime(new Date());
				borrowReturnMapper.updateBorrowReturnDetailToReturnByBorrowReturnId(detail);
				
				//插入资产生命周期表记录
				 AssetRecord re = new AssetRecord();
				 re.setId(IdWorker.getId());
				 re.setAssetId(Long.parseLong(detail.getAssetId_()));
				 re.setType(6);
				 re.setCreateBy(WebUtil.getCurrentUser());
				 re.setCreateTime(new Date());
				 assetRecordMapper.create(re);
			}
		}

		int count = borrowReturnMapper.queryNotReturnNum(borrowReturn.getId());
		if (count == 0) { // 如果已经全部归还将归还单状态置为已完成
			borrowReturnMapper.updateBorrowReturnToReturn(borrowReturn.getId());
		}

		List<BorrowReturnDetail> detailList = borrowReturnMapper
				.qryBorrowReturnDetailByBorrowReturnId(borrowReturn.getId());
		borrowReturnMapper.updateAssetIsNotBorrow(detailList);// 修改设备为非借用设备

		for (BorrowReturnDetail borrowReturnDetail : detailList) {
			// 若为内部借用，在确认借用时，将设备归属部门修改
			if (borrowReturnDetail.getBorrowType().equals(1)) {
				borrowReturnMapper.updateAssetExtUsingOriginal(borrowReturnDetail);
			}
		}
		// 外部借用时，需要再去通过上下架单去修改，这里不做改变
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Page<ItAssetVo> querAssetListByParam(Page<ItAssetVo> page, Map<String, Object> params) {
		page.setRecords(borrowReturnMapper.querAssetListByParam(page, params));
		return page;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public BorrowReturn qryBorrowListById(Long id) {
		BorrowReturn borrowReturn = borrowReturnMapper.qryById(id);
		List<BorrowReturnDetail> detailList = borrowReturnMapper.qryBorrowList(id);
		borrowReturn.setBorrowReturnDetail(detailList);
		return borrowReturn;
	}

}
